package com.example.day09question.net;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.io.File;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.example.day09question.bean.Mess;
import com.example.day09question.bean.RvBean;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
 * Created by 阿炜啊 on 2020/7/15.
 */

public class MyService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();///,
        final File file = new File("/sdcard/DCIM/Camera/52441603248696707211593841656104.jpg");//
              Retrofit build = new Retrofit.Builder()
                .baseUrl(ApiService.BASE)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiservice = build.create(ApiService.class);
        Observable<ResponseBody> observable = apiservice.getSs();
        observable.subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        InputStream in = responseBody.byteStream();
                        long max = responseBody.contentLength();
                        savaFlie(file,in,max);
                    }



                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    private static void savaFlie(File file, InputStream in, long max) {
        EventBus.getDefault().post(new Mess(1,(int)max,0,0));
        try {
            FileOutputStream out = new FileOutputStream(file);
            byte[] by=new byte[1024];
            int len=0;
            int count=0;
            while ((len=in.read(by))!=-1){
                out.write(by,0,len);
                count+=len;
                int  s=(int)((count/max)*100);
                EventBus.getDefault().post(new Mess(2,(int)max,count,s));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
